package com.hspedu.homework.homework02;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/3
 * @time 18:14
 */
public class Homework02SenderB {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(9998);

        byte[] data = "四大名著是那些".getBytes();
        DatagramPacket datagramPacket = new DatagramPacket(data, data.length, InetAddress.getLocalHost(),8888);
        datagramSocket.send(datagramPacket);


        byte[] answer = new byte[1024];
        DatagramPacket answerPacket = new DatagramPacket(answer, answer.length);
        datagramSocket.receive(answerPacket);
        String strDatagram = new String(answerPacket.getData(), answerPacket.getOffset(), answerPacket.getLength());
        System.out.println(strDatagram);

        datagramSocket.close();
    }
}
